package org.graduation.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.project.ProjectUserTable;
import org.graduation.model.common.user.User;
import org.graduation.project.mapper.ProjectUserMapper;
import org.graduation.project.service.ProjectAndUserService;
import org.graduation.utils.ResponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectAndUserServiceImpl extends ServiceImpl<ProjectUserMapper, ProjectUserTable> implements ProjectAndUserService {
    @Autowired
    private ProjectUserMapper projectUserMapper;

    @Override
    public ResponseResult join(ProjectUserTable projectUserTable) {
        return ResponseResultUtil.simpleCheck(projectUserMapper.insert(projectUserTable) == 1);
    }

    @Override
    public List<User> getUsers(Long pId) {
        return projectUserMapper.getUsersByProjectId(pId);
    }

    @Override
    public boolean existUserInProject(Long pId, Integer uid) {
        LambdaQueryWrapper<ProjectUserTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProjectUserTable::getUid, uid)
                .eq(ProjectUserTable::getPid, pId)
        ;
        return projectUserMapper.exists(queryWrapper);
    }
}
